﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 挂在对话UI上，根据现在说话者的名字使UI显示在对应人物头上
/// 如果说话者是玩家，则显示在玩家前方
/// </summary>
public class Talk_SetPosByName : MonoBehaviour {
	string lastName = "";
	ReadText rt;
	Transform playerTransform;
	//当选择时
	bool isSetPos = false;
	void Start(){
		rt = GetComponent<ReadText> ();
		playerTransform = StaticVarFunc.PlayerEye.transform;
	}
	void Update(){
		if (rt != null && lastName != rt.person) {
			lastName = rt.person;
			print (lastName == null);
			print (rt.person == null);
			setPositionByName (lastName);
		}
		transform.LookAt (transform.position + transform.position - playerTransform.position);
	}
	public void setPositionByName(string personName){
		//一定要注意，null和""输出是一样的，都是""！！！
		bool isNull = personName == null;
		personName = isNull ? "" : personName;
		foreach (Talk_NamePosition.Name_Position np in StaticVarFunc.namePos.nameList) {
			if (personName == np.nameInTxt) {
				if (np.isLocal) {
					transform.position = np.tran.localToWorldMatrix.MultiplyPoint3x4 (np.pianYi);
				} else {
					transform.position = np.tran.position + np.pianYi;
				}
				transform.LookAt (StaticVarFunc.PlayerEye.transform.position);
				transform.parent = isNull ? null : np.tran;
				return;
			}
		}
	}

}
